package pc

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

func NewUser(user map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	values, sql := InsertSql(TablePcUser, user)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

func UpdateUser(user map[string]interface{}, uid int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessCenter)
	condition := fmt.Sprintf(`uid = %v`, uid)
	values, sql := UpdateSql(TablePcUser, user, condition)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

func UserInfoByName(name string, multiOrm ...orm.Ormer) (*PcUser, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			name = ?
		LIMIT 1
		`, TablePcUser)
	o := NewOrm(multiOrm, DBChessCenter)
	var user PcUser
	err := o.Raw(sql, name).QueryRow(&user)
	if CheckNoExist(err) {
		return &user, nil
	}
	return &user, err
}

func UserInfoById(uid int64, multiOrm ...orm.Ormer) (*PcUser, error) {
	sql := fmt.Sprintf(`
		SELECT
			uid,role_id,group_id,email,real_name,name,frozen,create_time,mutable
		FROM
			%v
		WHERE
			uid = ?
		LIMIT 1
		`, TablePcUser)
	o := NewOrm(multiOrm, DBChessCenter)
	var user PcUser
	err := o.Raw(sql, uid).QueryRow(&user)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &user, err
}

//获取pc端的名字,做缓存
//TODO:要做成memory cache,接口有问题
func UserNameById(uid interface{}, multiOrm ...orm.Ormer) string {
	key := fmt.Sprintf("%v%v", KeyPcUserName, uid)
	name := RedisCache.GetString(key)
	if name == "" {
		sql := fmt.Sprintf(`
		SELECT
			name
		FROM
			%v
		WHERE
			uid = ?
		LIMIT 1
		`, TablePcUser)
		o := NewOrm(multiOrm, DBChessCenter)
		var name string
		o.Raw(sql, uid).QueryRow(&name)
		RedisCache.Put(key, name, 120)
	}
	return name
}

//pc管理用户信息
func ListUserInfo(p ListPcUserParams, multiOrm ...orm.Ormer) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	where := " 1=1 "
	if p.Name != "" {
		where = where + fmt.Sprintf(" AND u.name LIKE '%v' ", "%"+p.Name+"%")
	}
	sql := fmt.Sprintf(`
		SELECT
			u.uid,u.role_id,u.group_id,u.email,u.name,u.real_name,u.frozen,u.create_time,u.login_time,
			g.name AS group_name,r.name AS role_name
		FROM
			%v AS u
		LEFT JOIN
			%v AS g
		ON u.group_id = g.id
		LEFT JOIN
			%v AS r
		ON u.role_id = r.id
		WHERE
			%v
		LIMIT ?,?
		`, TablePcUser, TablePcGroup, TablePcRole, where)
	o := NewOrm(multiOrm, DBChessCenter)
	var user []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&user)
	if err != nil {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v AS u WHERE %v `, TablePcUser, where)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return user, count, err
}

//根据用户名字查询用户是否已经存在
func IsUserExist(name string, multiOrm ...orm.Ormer) (*PcUser, bool) {
	sql := fmt.Sprintf(`
		SELECT
			uid
		FROM
			%v
		WHERE
			name = ?
		LIMIT 1
		`, TablePcUser)
	o := NewOrm(multiOrm, DBChessCenter)
	var user PcUser
	err := o.Raw(sql, name).QueryRow(&user)
	if err == orm.ErrNoRows {
		return nil, false
	}
	return &user, true
}
